package com.yinhe.music.yhmusic.dialog;

import android.app.Activity;
import android.content.DialogInterface;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.widget.Toast;

import com.yinhe.music.yhmusic.utils.permission.PermissionReq;
import com.yinhe.music.yhmusic.utils.permission.PermissionResult;
import com.yinhe.music.yhmusic.utils.permission.Permissions;

public class CameraDialog {
    private Activity mContext;
    private static final String[] ITEMS = {"照相", "从相册中选择"};
    private AlertDialog.Builder listDialog;
    private DialogInterface.OnClickListener mListener;
    private final int ALBUM = 1, CAMERA = 0;
    private TakePhotoListener takePhotoListener;

    public interface TakePhotoListener {
        void didTakePhoto();
        void didSelectPhoto();
    }

    public CameraDialog(@NonNull Activity context, TakePhotoListener listener) {
        mContext = context;
        takePhotoListener = listener;
        init();
    }

    public void show() {
        if (listDialog != null) {
            listDialog.show();
        }
    }

    private void init() {
        listDialog = new AlertDialog.Builder(mContext);
        listDialog.setTitle("更换头像");
        initListener();
        listDialog.setItems(ITEMS, mListener);
    }

    private void initListener() {
        mListener = (dialog, which) -> {
            switch (which) {
                case ALBUM:
                    takePhotoListener.didSelectPhoto();
                    break;
                case CAMERA:
                    reqCamera();
                    break;
                default:
                    break;
            }
        };
    }

    private void reqCamera() {
        PermissionReq.with(mContext)
            .permissions(Permissions.CAMERA)
            .result(new PermissionResult() {
                @Override
                public void onGranted() {
                    takePhotoListener.didTakePhoto();
                }

                @Override
                public void onDenied() {
                    Toast.makeText(mContext, "很遗憾你把相机权限禁用了。请务必开启相机权限享受我们提供的服务吧。", Toast.LENGTH_SHORT)
                            .show();
                }
            })
            .request();
    }
}
